<template>
  <div class="u-p-20">
    <el-card shadow="never">
      <div slot="header">
        <b class="u-f-16">商品分类列表</b>
        <router-link :to="{ name: 'goodsTypeEdit' }">
          <el-button type="danger" size="small" icon="el-icon-plus" class="u-right u-5mt" round>添加商品分类</el-button>
        </router-link>
      </div>
      <el-form :inline="true" :model="queryForm" size="small" class="u-text-center">
        <el-form-item label="商品分类状态" style="margin-left: 100px;">
          <el-select @change="handleChange()" v-model="queryForm.status" clearable>
            <el-option label="全部" value="-1"></el-option>
            <el-option label="启用" value="0"></el-option>
            <el-option label="禁用" value="1"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="是否敏感">
          <el-select @change="handleChange()" v-model="queryForm.special" clearable>
            <el-option label="全部" value="-1"></el-option>
            <el-option label="敏感分类" value="1"></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="getTreeDatas()">查询</el-button>
        </el-form-item>
      </el-form>
      <el-button size="mini" type="primary" @click="handleStatusIds(1)">批量启用</el-button>
      <el-button size="mini" type="warning" @click="handleStatusIds(0)">批量禁用</el-button>
      <el-table :data="tableData" row-key="id" class="u-mt-6" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="分类名称" prop="name"></el-table-column>
        <el-table-column label="售价上浮百分比">
          <template slot-scope="scope">
            <el-tag type="success">{{ scope.row.go_up }}%</el-tag>
          </template>
        </el-table-column>
        <!-- <el-table-column label="是否敏感商品">
          <template slot-scope="scope">
            <el-tag type="success" v-if="scope.row.special == 0">不是</el-tag>
            <el-tag type="danger" v-if="scope.row.special == 1">是</el-tag>
          </template>
        </el-table-column> -->
        <el-table-column label="排序" prop="sort"></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status" :active-value="0" :inactive-value="1" active-color="#13ce66"
              @change="handleStatus(scope.row.id, scope.row.status)"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handlePidRoute(scope.row.id, 'goodsTypeEdit')">添加子类</el-button>
            <el-button size="mini" type="text" @click="handleRoute(scope.row.id, 'goodsTypeEdit')">编辑</el-button>
            <el-button size="mini" type="text" @click="handleDelete(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import commonPage from "@/mixins/common-page";

export default {
  mixins: [commonPage],
  data() {
    return {
      apiName: "goodsType",
      isTree: true,
      queryForm: {
        status: "-1",
        special: "-1"
      },
      idArray: []
    };
  },
  methods: {
    // 下拉事件
    handleChange() {
      this.getTreeDatas();
    },
    getTreeDatas() {
      this.$api[this.apiName]
        .tree(this.queryForm)
        .then(response => {
          if (response.code == 1) {
            this.tableData = response.data.treeData;
          }
        })
        .catch(() => { });
    },
    handleSelectionChange(val) {
      val.map(item => {
        this.idArray.push(item.id);
      });
    },
    handleStatusIds(val) {
      if (this.idArray == "") {
        this.$message({
          type: "error",
          message: "请选择要操作的数据!"
        });
      } else {
        if (val == 1) {
          this.$confirm("确认启用吗?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
            .then(() => {
              this.$api[this.apiName].status(this.idArray, 0).then(response => {
                if (response.code == 1) {
                  this.$message({
                    type: "success",
                    message: "启用成功!"
                  });
                  this.onLoad();
                }
              });
            })
            .catch(() => { });
        } else {
          this.$confirm("确认禁用吗?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
            .then(() => {
              this.$api[this.apiName].status(this.idArray, 1).then(response => {
                if (response.code == 1) {
                  this.$message({
                    type: "success",
                    message: "禁用成功!"
                  });
                  this.onLoad();
                }
              });
            })
            .catch(() => { });
        }
      }
    }
  }
};
</script>
